﻿using System.Xml;

namespace Common
{
    public static class XmlHelper
    {
        /// <summary>
        /// 设置节点内容
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="innerText"></param>
        public  static void SetInnerText(this XmlNode xmlNode,string xmlPath,string innerText)
        {
            var node = xmlNode.SelectSingleNode(xmlPath);

            if (node != null)
            {
                node.InnerText = innerText;
            }
        }

        /// <summary>
        /// 获取节点内容
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        public static string GetInnerText(this XmlNode xmlNode, string xmlPath)
        {
            string innerText = "";

            var node = xmlNode.SelectSingleNode(xmlPath);
            if (node != null)
            {
                innerText = node.InnerText;
            }

            return innerText;
        }

        /// <summary>
        /// 获取属性内容
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static string GetAttributeText(this XmlNode xmlNode, string xmlPath, string attributeName)
        {
            string innerText = "";

            var node = xmlNode.SelectSingleNode(xmlPath);
            if (node != null && node.Attributes[attributeName]!=null)
            {
                innerText = node.Attributes[attributeName].InnerText;
            }

            return innerText;
        }

        /// <summary>
        /// 设置属性内容
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="attributeName"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public static void SetAttributeText(this XmlNode xmlNode, string xmlPath, string attributeName,string innerText)
        {
            var node = xmlNode.SelectSingleNode(xmlPath);
            if (node != null && node.Attributes[attributeName] != null)
            {
                node.Attributes[attributeName].InnerText= innerText;
            }
        }

        /// <summary>
        /// 获取节点内容（根节点带命名空间）
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="namespaceContent"></param>
        /// <returns></returns>
        public static string GetInnerTextWithNamespace(this XmlNode xmlNode, string xmlPath,string namespaceContent)
        {
            string innerText = "";

            XmlDocument xmlDocument =  xmlNode as XmlDocument;
            if (xmlDocument != null)
            {
                XmlElement resume = xmlDocument.DocumentElement;
                XmlNamespaceManager nameSpace = new XmlNamespaceManager(xmlDocument.NameTable);
                nameSpace.AddNamespace("A", namespaceContent);
                xmlPath = xmlPath.Replace("/", "/A:");
                var node = resume.SelectSingleNode(xmlPath, nameSpace);
                if (node != null)
                {
                    innerText = node.InnerText;
                }
            }

            return innerText;
        }

        /// <summary>
        /// 设置节点内容（根节点带命名空间）
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="innerText"></param>
        /// <param name="namespaceContent"></param>
        public static void SetInnerTextWithNamespace(this XmlNode xmlNode, string xmlPath, string innerText,string namespaceContent)
        {
            XmlDocument xmlDocument = xmlNode as XmlDocument;
            if (xmlDocument != null)
            {
                XmlElement resume = xmlDocument.DocumentElement;
                XmlNamespaceManager nameSpace = new XmlNamespaceManager(xmlDocument.NameTable);
                nameSpace.AddNamespace("A", namespaceContent);
                xmlPath = xmlPath.Replace("/", "/A:");
                var node = resume.SelectSingleNode(xmlPath, nameSpace);
                if (node != null)
                {
                    node.InnerText = innerText;
                }
            }
        }

        /// <summary>
        /// 获取属性内容（根节点带命名空间）
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="attributeName"></param>
        /// <param name="namespaceContent"></param>
        /// <returns></returns>
        public static string GetAttributeTextWithNamespace(this XmlNode xmlNode, string xmlPath, string attributeName, string namespaceContent)
        {
            string innerText = "";

            XmlDocument xmlDocument = xmlNode as XmlDocument;
            if (xmlDocument != null)
            {
                XmlElement resume = xmlDocument.DocumentElement;
                XmlNamespaceManager nameSpace = new XmlNamespaceManager(xmlDocument.NameTable);
                nameSpace.AddNamespace("A", namespaceContent);
                xmlPath = xmlPath.Replace("/", "/A:");
                var node = resume.SelectSingleNode(xmlPath, nameSpace);
                if (node != null)
                {
                    innerText = node.Attributes[attributeName].InnerText;
                }
            }

            return innerText;
        }

        /// <summary>
        /// 设置属性内容（根节点带命名空间）
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <param name="xmlPath"></param>
        /// <param name="attributeName"></param>
        /// <param name="innerText"></param>
        /// <param name="namespaceContent"></param>
        public static void SetAttributeTextWithNamespace(this XmlNode xmlNode, string xmlPath, string attributeName, string innerText, string namespaceContent)
        {
            XmlDocument xmlDocument = xmlNode as XmlDocument;
            if (xmlDocument != null)
            {
                XmlElement resume = xmlDocument.DocumentElement;
                XmlNamespaceManager nameSpace = new XmlNamespaceManager(xmlDocument.NameTable);
                nameSpace.AddNamespace("A", namespaceContent);
                xmlPath = xmlPath.Replace("/", "/A:");
                var node = resume.SelectSingleNode(xmlPath, nameSpace);
                if (node != null)
                {
                    node.Attributes[attributeName].InnerText = innerText;
                }
            }
        }






    }
}
